<?php
import("ORG.Util.Function");
import("@.ORrg.Utils");
import("@.Org.GroupbuyTool");


class GroupbuyAction extends CommonAction{	
	
	public function _initialize(){
		parent::_initialize();				
		$param           =    $this->_get('_URL_');
		$controller      =    $param[0];
		$action          =    $param[1];		
		//language
		$lang            =    L($action);  		
		$manage          =    L('manage');
		
		$this->assign("manage",$manage);
		$this->assign("current",$lang);
		$this->assign("lang",$lang);
	}
	
	/**
	 * 团购列表
	 * Enter description here ...
	 */
	public function groupbuy_list(){
			
	    $sql_groupbuy_list = get_sql_group_buy_list();
		/*$sort_flag  = sort_flag($list['filter']);
	    $this->assign($sort_flag['tag'], $sort_flag['img']); // 默认排序字段*/
	
	    $param = array(
				'result'	=>	$sql_groupbuy_list,			//分页用的数组或sql
				'listvar'	=>	'group_buy_list',				//分页循环变量
				'listRows'	=>	10,							//每页记录数
				//'parameter'=>'cat_id=' . $cat_id . "&intro_type=" . $intro_type . "&invalid=" . $invalid,//url分页后继续带的参数
				'target'	=>	'content',	    			//ajax更新内容的容器id，不带#
				'pagesId'	=>	'page',						//分页后页的容器id不带# target和pagesId同时定义才Ajax分页
				'template'	=>	'Groupbuy:group_buy_list',		//ajax更新模板
		);		
	    $this->page_groupbuy_list($param);	    
		$this->display('group_buy_list');
	}
	
	/**
	 * 团购分页
	 * Enter description here ...
	 * @param unknown_type $param
	 * 
	 */
	public function page_groupbuy_list($param){
		extract($param);
		//总记录数
		$flag = is_string($result);
		
		$listvar = $listvar ? $listvar : 'list';
		$listRows = $listRows? $listRows : 21;
		if ($flag){
			$totalRows	=	count(M()->query($result));
		}			
		else{
			$totalRows = ($result) ? count($result) : 1;
		}
		//创建分页对象
		if ($target && $pagesId)
			$p = new Page($totalRows, $listRows, $parameter, $url,$target, $pagesId);
		else
			$p = new Page($totalRows, $listRows, $parameter,$url);
		//抽取数据
		if ($flag) {
			$result .= " LIMIT {$p->firstRow},{$p->listRows}";
			$voList = M()->query($result);
		} else {
			$voList = array_slice($result, $p->firstRow, $p->listRows);
		}
		$pages = C('PAGE');//要ajax分页配置PAGE中必须theme带%ajax%，其他字符串替换统一在配置文件中设置，
		//===================================================== 格式化数据
		
		$lang_gbs			=	L('gbs');	
	    $list 				= 	array();    
	    foreach($voList as $row){
	    	$ext_info 		= 	unserialize($row['ext_info']);
	        $stat 			= 	group_buy_stat($row['act_id'], $ext_info['deposit']);
	        $arr 			= 	array_merge($row, $stat, $ext_info);
	
	        /* 处理价格阶梯 */
	        $price_ladder 	=	$arr['price_ladder'];
	        if (!is_array($price_ladder) || empty($price_ladder)){
	            $price_ladder 	= 	array(array('amount' => 0, 'price' => 0));
	        }
	        else{
	            foreach ($price_ladder AS $key => $amount_price){
	                $price_ladder[$key]['formated_price'] 	= 	price_format($amount_price['price']);
	            }
	        }
	
	        /* 计算当前价 */
	        $cur_price  	= 	$price_ladder[0]['price'];    // 初始化
	        $cur_amount 	= 	$stat['valid_goods'];         // 当前数量
	        foreach ($price_ladder AS $amount_price){
	            if ($cur_amount >= $amount_price['amount']){
	                $cur_price 	= 	$amount_price['price'];
	            }
	            else{
	                break;
	            }
	        }	
	        $arr['cur_price']   = 	$cur_price;	
	        $status 			= 	group_buy_status($arr);	
	        $arr['start_time']  = 	local_date($GLOBALS['_CFG']['date_format'], $arr['start_time']);
	        $arr['end_time']    = 	local_date($GLOBALS['_CFG']['date_format'], $arr['end_time']);
	        $arr['cur_status']  = 	$lang_gbs[$status];
	        $list[] = $arr;
	    }
	    
	    $voList					=	$list;
	    
		//===================================================== 格式化数据
		//可以使用该方法前用C临时改变配置
		foreach ($pages as $key => $value) {
			$p->setConfig($key, $value); // 'theme'=>'%upPage% %linkPage% %downPage% %ajax%'; 要带 %ajax%
		}
		//分页显示
		$page = $p->show();		
		//模板赋值
		$this->assign($listvar, $voList);
		$this->assign("page", $page);
		if ($this->isAjax()) {//判断ajax请求
			layout(false);
			$template = (!$template) ? 'ajaxlist' : $template;
			exit($this->fetch($template));
		}
		return $voList;
	}
	
	/**
	 * 添加团购
	 * Enter description here ...
	 */
	public function add_groupbuy(){		
		$GoodsModel			=	new GoodsModel();
		/* 初始化/取得团购活动信息 */
        $group_buy = array(
            'act_id'  => 0,
            'start_time'    => date('Y-m-d', time() + 86400),
            'end_time'      => date('Y-m-d', time() + 4 * 86400),
            'price_ladder'  => array(array('amount' => 0, 'price' => 0))
        );
        
	    $this->assign('group_buy', 		$group_buy);	
	    $this->assign('cat_list', 		$GoodsModel->cat_list());
	    $this->assign('brand_list', 	get_brand_list());
	    $this->assign('action', 		"add_groupbuy_ok");
		
		$this->display("group_buy_info");
	} 
	
	/**
	 * 处理添加团购
	 * 
	 */
	public function add_groupbuy_ok(){
		$CommonModel	=	new CommonModel();
		
		/* 取得团购活动id */
    	$group_buy_id 	= 	intval($_POST['act_id']);
		
	 	/* 保存团购信息 */
        $goods_id 		= 	intval($_POST['goods_id']);
        if ($goods_id <= 0){
            $this->error(L('error_goods_null'));
        }
        
        $info 			= 	goods_group_buy($goods_id);
        if ($info && $info['act_id'] != $group_buy_id){
            $this->error(L('error_goods_exist'));
        }

        $goods_name 	= 	$CommonModel->getOne("SELECT goods_name FROM " . 'ybr_goods' . " WHERE goods_id = '$goods_id'");
        
        $act_name 		= 	empty($_POST['act_name']) ? $goods_name : sub_str($_POST['act_name'], 0, 255, false);

        $deposit 		= 	floatval($_POST['deposit']);
        if ($deposit < 0){
            $deposit = 0;
        }

        $restrict_amount 	= 	intval($_POST['restrict_amount']);
        if ($restrict_amount < 0){
            $restrict_amount 	= 	0;
        }

        $gift_integral 	= 	intval($_POST['gift_integral']);
        if ($gift_integral < 0){
            $gift_integral 	= 	0;
        }

        $price_ladder 	= 	array();
        $count 			= 	count($_POST['ladder_amount']);
        for ($i = $count - 1; $i >= 0; $i--){
            /* 如果数量小于等于0，不要 */
            $amount 	= 	intval($_POST['ladder_amount'][$i]);
            if ($amount <= 0){
                continue;
            }

            /* 如果价格小于等于0，不要 */
            $price = round(floatval($_POST['ladder_price'][$i]), 2);
            if ($price <= 0){
                continue;
            }

            /* 加入价格阶梯 */
            $price_ladder[$amount] 	= 	array('amount' => $amount, 'price' => $price);
        }
        if (count($price_ladder) < 1){
            //sys_msg($_LANG['error_price_ladder']);
            $this->error(L('error_price_ladder'));
        }

        /* 限购数量不能小于价格阶梯中的最大数量 */
        $amount_list = array_keys($price_ladder);
        if ($restrict_amount > 0 && max($amount_list) > $restrict_amount){
            sys_msg($_LANG['error_restrict_amount']);
        }

        ksort($price_ladder);
        $price_ladder 				= 	array_values($price_ladder);

        /* 检查开始时间和结束时间是否合理 */
        $start_time 				= 	local_strtotime($_POST['start_time']);
        $end_time 					= 	local_strtotime($_POST['end_time']);
        if ($start_time >= $end_time){
            $this->error(L('invalid_time'));
        }

        $group_buy = array(
            'act_name'   	=> $act_name,
            'act_desc'   	=> $_POST['act_desc'],
            'act_type'   	=> GAT_GROUP_BUY,
            'goods_id'  	=> $goods_id,
            'goods_name' 	=> $goods_name,
            'start_time'    => $start_time,
            'end_time'      => $end_time,
            'ext_info'   	=> serialize(
        		array(
                    'price_ladder'      => $price_ladder,
                    'restrict_amount'   => $restrict_amount,
                    'gift_integral'     => $gift_integral,
                    'deposit'           => $deposit
                )
            )
        );        
		/* insert */
		$CommonModel->autoExecute('goods_activity', $group_buy, 'INSERT');	// 前面不加表前缀		
		/* log */
		admin_log(addslashes($goods_name), 'add', 'group_buy');	
		$this->success("操作成功",U('groupbuy/add_groupbuy'));
	}

	public function mod_groupbuy(){
		$GoodsModel			=	new GoodsModel();
		
		$group_buy_id 		= 	intval($_REQUEST['id']);
        if ($group_buy_id <= 0){
            die('invalid param');
        }
        $group_buy 			= 	group_buy_info($group_buy_id);
		
		$this->assign('group_buy', 		$group_buy);	
	    $this->assign('cat_list', 		$GoodsModel->cat_list());
	    $this->assign('brand_list', 	get_brand_list());
	    $this->assign('action', 		"mod_groupbuy_ok");
		
		$this->display("group_buy_info");
	}
	
	/**
	 * 处理修改团购信息
	 * Enter description here ...
	 */
	public function mod_groupbuy_ok(){		
			
		$CommonModel		=	new CommonModel();
		/* 取得团购活动id */
	    $group_buy_id 		= 	intval($_POST['act_id']);
	    
	    if (isset($_POST['finish']) || isset($_POST['succeed']) || isset($_POST['fail']) || isset($_POST['mail'])){
	        if ($group_buy_id <= 0){
	            $this->error(L('error_group_buy'));
	        }
	        $group_buy 		= 	group_buy_info($group_buy_id);
	        if (empty($group_buy)){
	            //sys_msg($_LANG['error_group_buy'], 1);
	            $this->error(L('error_group_buy'));
	        }
	    }
	
	    if (isset($_POST['finish'])){
	        /* 判断订单状态 */
	        if ($group_buy['status'] != GBS_UNDER_WAY){
	            $this->error(L('error_status'));
	        }	
	        /* 结束团购活动，修改结束时间为当前时间 */
	        $sql 			= 	"UPDATE " . 'ybr_goods_activity' .
	                			" SET end_time = '" . gmtime() . "' " .
	                			"WHERE act_id = '$group_buy_id' LIMIT 1";
	        $CommonModel->query($sql);	
	        /* 清除缓存 
	        clear_cache_files();*/	        
	        $this->success("操作成功",U('groupbuy/groupbuy_list'));
	        
	    }
	    elseif (isset($_POST['succeed'])){
	        /* 设置活动成功 */	
	        /* 判断订单状态 */
	        if ($group_buy['status'] != GBS_FINISHED){
	            $this->error(L('error_status'));
	        }
	
	        /* 如果有订单，更新订单信息 */
	        if ($group_buy['total_order'] > 0){
	            /* 查找该团购活动的已确认或未确认订单（已取消的就不管了） */
	            $sql 			= 	"SELECT order_id " .
	                    			"FROM " . 'ybr_order_info' .
	                    			" WHERE extension_code = 'group_buy' " .
	                    			"AND extension_id = '$group_buy_id' " .
	                    			"AND (order_status = '" . OS_CONFIRMED . "' or order_status = '" . OS_UNCONFIRMED . "')";
	            $order_id_list 	= 	$CommonModel->getCol($sql);
	
	            /* 更新订单商品价 */
	            $final_price 	= 	$group_buy['trans_price'];
	            $sql 			= 	"UPDATE " . 'ybr_order_goods' .
	                    			" SET goods_price = '$final_price' " .
	                    			"WHERE order_id " . db_create_in($order_id_list);
	            $CommonModel->query($sql);
	
	            /* 查询订单商品总额 */
	            $sql 			= 	"SELECT order_id, SUM(goods_number * goods_price) AS goods_amount " .
	                    			"FROM " . 'ybr_order_goods' .
	                    			" WHERE order_id " . db_create_in($order_id_list) .
	                    			" GROUP BY order_id";
	            $res 			= 	$CommonModel->query($sql);
	            
	            foreach($res as $row){
	            	$order_id 		= 	$row['order_id'];
	                $goods_amount 	= 	floatval($row['goods_amount']);
	
	                /* 取得订单信息 */
	                $order 			= 	order_info($order_id);
	
	                /* 判断订单是否有效：余额支付金额 + 已付款金额 >= 保证金 */
	                if ($order['surplus'] + $order['money_paid'] >= $group_buy['deposit']){
	                    /* 有效，设为已确认，更新订单 */
	
	                    // 更新商品总额
	                    $order['goods_amount'] 		= 	$goods_amount;
	
	                    // 如果保价，重新计算保价费用
	                    if ($order['insure_fee'] > 0){
	                        $shipping 				= 	shipping_info($order['shipping_id']);
	                        $order['insure_fee'] 	= 	shipping_insure_fee($shipping['shipping_code'], $goods_amount, $shipping['insure']);
	                    }
	
	                    // 重算支付费用
	                    $order['order_amount'] 		= 	$order['goods_amount'] + $order['shipping_fee'] + $order['insure_fee'] + $order['pack_fee'] + $order['card_fee'] - $order['money_paid'] - $order['surplus'];
	                    if ($order['order_amount'] > 0){
	                        $order['pay_fee'] 		= 	pay_fee($order['pay_id'], $order['order_amount']);
	                    }
	                    else{
	                        $order['pay_fee'] = 0;
	                    }
	
	                    // 计算应付款金额
	                    $order['order_amount'] 		+= 	$order['pay_fee'];
	
	                    // 计算付款状态
	                    if ($order['order_amount'] > 0){
	                        $order['pay_status'] 	= 	PS_UNPAYED;
	                        $order['pay_time'] 		= 	0;
	                    }
	                    else{
	                        $order['pay_status'] 	= 	PS_PAYED;
	                        $order['pay_time'] 		= 	gmtime();
	                    }
	
	                    // 如果需要退款，退到帐户余额
	                    if ($order['order_amount'] < 0){
	                        // todo （现在手工退款）
	                    }
	
	                    // 订单状态
	                    $order['order_status'] 		= 	OS_CONFIRMED;
	                    $order['confirm_time'] 		= 	gmtime();	
	                    // 更新订单
	                    $order 						= 	addslashes_deep($order);
	                    update_order($order_id, $order);
	                }
	                else{
	                    /* 无效，取消订单，退回已付款 */
	
	                    // 修改订单状态为已取消，付款状态为未付款
	                    $order['order_status'] 		= 	OS_CANCELED;
	                    $order['to_buyer'] 			= 	L('cancel_order_reason');
	                    $order['pay_status'] 		= 	PS_UNPAYED;
	                    $order['pay_time'] 			= 	0;
	
	                    /* 如果使用余额或有已付款金额，退回帐户余额 */
	                    $money 						= 	$order['surplus'] + $order['money_paid'];
	                    if ($money > 0){
	                        $order['surplus'] 		= 	0;
	                        $order['money_paid'] 	= 	0;
	                        $order['order_amount'] 	= 	$money;
	
	                        // 退款到帐户余额
	                        order_refund($order, 1, L('cancel_order_reason') . ':' . $order['order_sn']);
	                    }	
	                    /* 更新订单 */
	                    $order 						= 	addslashes_deep($order);
	                    update_order($order['order_id'], $order);
	                }
	            }
	        }
	
	        /* 修改团购活动状态为成功 */
	        $sql 									= 	"UPDATE " . 'ybr_goods_activity' .
	                									" SET is_finished = '" . GBS_SUCCEED . "' " .
	                									"WHERE act_id = '$group_buy_id' LIMIT 1";
	        $CommonModel->query($sql);
	
	        /* 清除缓存 
	        clear_cache_files();*/	        
	        $this->success("操作成功",U('groupbuy/groupbuy_list'));	        
	    }
	    elseif (isset($_POST['fail'])){
	        /* 设置活动失败 */
	
	        /* 判断订单状态 */
	        if ($group_buy['status'] != GBS_FINISHED){
	            $this->error(L('error_status'));
	        }
	
	        /* 如果有有效订单，取消订单 */
	        if ($group_buy['valid_order'] > 0){
	            /* 查找未确认或已确认的订单 */
	            $sql 								= 	"SELECT * " .
	                    								"FROM " . 'ybr_order_info' .
	                    								" WHERE extension_code = 'group_buy' " .
	                    								"AND extension_id = '$group_buy_id' " .
	                    								"AND (order_status = '" . OS_CONFIRMED . "' OR order_status = '" . OS_UNCONFIRMED . "') ";
	            $res 								= 	$CommonModel->query($sql);
	            
	            foreach($res as $order){
	            	// 修改订单状态为已取消，付款状态为未付款
	                $order['order_status'] 			= 	OS_CANCELED;
	                $order['to_buyer'] 				= 	L('cancel_order_reason');
	                $order['pay_status'] 			=	PS_UNPAYED;
	                $order['pay_time'] 				= 	0;
	
	                /* 如果使用余额或有已付款金额，退回帐户余额 */
	                $money 							= 	$order['surplus'] + $order['money_paid'];
	                if ($money > 0){
	                    $order['surplus'] 			= 	0;
	                    $order['money_paid'] 		= 	0;
	                    $order['order_amount'] 		= 	$money;	
	                    // 退款到帐户余额
	                    order_refund($order, 1, L('cancel_order_reason') . ':' . $order['order_sn'], $money);
	                }
	
	                /* 更新订单 */
	                $order 							= 	addslashes_deep($order);
	                update_order($order['order_id'], $order);
	            }
	        }
	
	        /* 修改团购活动状态为失败，记录失败原因（活动说明） */
	        $sql 									= 	"UPDATE " . 'ybr_goods_activity' .
	                									" SET is_finished = '" . GBS_FAIL . "', " .
	                    								"act_desc = '$_POST[act_desc]' " .
	                									"WHERE act_id = '$group_buy_id' LIMIT 1";
	        $CommonModel->query($sql);
	
	        /* 清除缓存 
	        clear_cache_files();*/
	        $this->success("操作成功",U('groupbuy/groupbuy_list'));	        
	        
	    }
	    elseif (isset($_POST['mail'])){
	        /* 发送通知邮件 */
	
	        /* 判断订单状态 */
	        if ($group_buy['status'] != GBS_SUCCEED){
	            sys_msg($_LANG['error_status'], 1);
	        }
	
	        /* 取得邮件模板 */
	        $tpl 									= 	get_mail_template('group_buy');
	
	        /* 初始化订单数和成功发送邮件数 */
	        $count 									= 	0;
	        $send_count 							= 	0;
	
	        /* 取得有效订单 */
	        $sql 									= 	"SELECT o.consignee, o.add_time, g.goods_number, o.order_sn, " .
	                    								"o.order_amount, o.order_id, o.email " .
	                									"FROM " . 'ybr_order_info' . " AS o, " .
	                    								'ybr_order_goods' . " AS g " .
	                									"WHERE o.order_id = g.order_id " .
	                									"AND o.extension_code = 'group_buy' " .
	                									"AND o.extension_id = '$group_buy_id' " .
	                									"AND o.order_status = '" . OS_CONFIRMED . "'";
	        $res 									= 	$db->query($sql);
	        
	        foreach($res as $order){
	        	 /* 邮件模板赋值 */
	            $smarty->assign('consignee',    $order['consignee']);
	            $smarty->assign('add_time',     local_date($_CFG['time_format'], $order['add_time']));
	            $smarty->assign('goods_name',   $group_buy['goods_name']);
	            $smarty->assign('goods_number', $order['goods_number']);
	            $smarty->assign('order_sn',     $order['order_sn']);
	            $smarty->assign('order_amount', price_format($order['order_amount']));
	            $smarty->assign('shop_url',     $ecs->url() . 'user.php?act=order_detail&order_id='.$order['order_id']);
	            $smarty->assign('shop_name',    $_CFG['shop_name']);
	            $smarty->assign('send_date',    local_date($_CFG['date_format']));
	
	            /* 取得模板内容，发邮件 */
	            $content = $smarty->fetch('str:' . $tpl['template_content']);
	            if (send_mail($order['consignee'], $order['email'], $tpl['template_subject'], $content, $tpl['is_html']))
	            {
	                $send_count++;
	            }
	            $count++;
	        }	
	        
	        $this->success("操作chenggong",U('groupbuy/groupbuy_list'));
	    }
	    else{
	        /* 保存团购信息 */
	        $goods_id 				= 	intval($_POST['goods_id']);
	        if ($goods_id <= 0){
	            //sys_msg($_LANG['error_goods_null']);
	            $this->error(L('error_goods_null'));
	        }
	        $info 					= 	goods_group_buy($goods_id);
	        if ($info && $info['act_id'] != $group_buy_id){
	            //sys_msg($_LANG['error_goods_exist']);
	            $this->error(L('error_goods_exist'));
	        }
	
	        $goods_name 			= 	$CommonModel->getOne("SELECT goods_name FROM " . 'ybr_goods' . " WHERE goods_id = '$goods_id'");
	
	        $act_name 				= 	empty($_POST['act_name']) ? $goods_name : sub_str($_POST['act_name'], 0, 255, false);
	
	        $deposit = floatval($_POST['deposit']);
	        if ($deposit < 0){
	            $deposit 			= 	0;
	        }
	
	        $restrict_amount = intval($_POST['restrict_amount']);
	        if ($restrict_amount < 0){
	            $restrict_amount 	= 	0;
	        }
	
	        $gift_integral = intval($_POST['gift_integral']);
	        if ($gift_integral < 0){
	            $gift_integral 		= 	0;
	        }
	
	        $price_ladder 			= 	array();
	        $count 					= 	count($_POST['ladder_amount']);
	        for ($i = $count - 1; $i >= 0; $i--){
	            /* 如果数量小于等于0，不要 */
	            $amount 			= 	intval($_POST['ladder_amount'][$i]);
	            if ($amount <= 0){
	                continue;
	            }
	
	            /* 如果价格小于等于0，不要 */
	            $price = round(floatval($_POST['ladder_price'][$i]), 2);
	            if ($price <= 0){
	                continue;
	            }
	
	            /* 加入价格阶梯 */
	            $price_ladder[$amount] = array('amount' => $amount, 'price' => $price);
	        }
	        
	        if (count($price_ladder) < 1){
	            //sys_msg($_LANG['error_price_ladder']);
	            $this->error(L('error_price_ladder'));
	        }
	
	        /* 限购数量不能小于价格阶梯中的最大数量 */
	        $amount_list 			= 	array_keys($price_ladder);
	        if ($restrict_amount > 0 && max($amount_list) > $restrict_amount){
	            //sys_msg($_LANG['error_restrict_amount']);
	            $this->error(L('error_restrict_amount'));
	        }
	
	        ksort($price_ladder);
	        $price_ladder 			= 	array_values($price_ladder);
	
	        /* 检查开始时间和结束时间是否合理 */
	        $start_time = local_strtotime($_POST['start_time']);
	        $end_time = local_strtotime($_POST['end_time']);
	        if ($start_time >= $end_time){
	            sys_msg($_LANG['invalid_time']);
	        }
	
	        $group_buy = array(
	            'act_name'   	=> $act_name,
	            'act_desc'   	=> $_POST['act_desc'],
	            'act_type'   	=> GAT_GROUP_BUY,
	            'goods_id'   	=> $goods_id,
	            'goods_name' 	=> $goods_name,
	            'start_time'    => $start_time,
	            'end_time'      => $end_time,
	            'ext_info'   	=> serialize(
	        		array(
	                    'price_ladder'      => $price_ladder,
	                    'restrict_amount'   => $restrict_amount,
	                    'gift_integral'     => $gift_integral,
	                    'deposit'           => $deposit
	                )
	            )
	        );
	
	        /* 清除缓存
	        clear_cache_files(); */	
	        /* 保存数据 */
	        if ($group_buy_id > 0){
	            /* update */
	            $CommonModel->autoExecute('goods_activity', $group_buy, 'UPDATE', "act_id = '$group_buy_id'");
	
	            /* log */
	            admin_log(addslashes($goods_name) . '[' . $group_buy_id . ']', 'edit', 'group_buy');
	
	            /* todo 更新活动表 */
	            	
	            $this->success("操作成功",U('groupbuy/groupbuy_list'));
	        }
	    }
	}
	
	/**
	 * 搜索商品
	 * Enter description here ...
	 */
	public function search_goods(){	
	    $filter 		= 	$_GET['JSON'];
	    $arr    		= 	get_goods_list($filter);
		$return['content']	=	$arr;
		$return['error']	=	0;
		$return['message']	=	"";	    
		$this->ajaxReturn($return);
	}
	
	/**
	 * 删除团购
	 * Enter description here ...
	 */
	public function remove_groupbuy(){
		
		$CommonModel	=	new CommonModel();		
	    $id 			= 	intval($_GET['id']);	
	    /* 取得团购活动信息 */
	    $group_buy 		= 	group_buy_info($id);
	
	    /* 如果团购活动已经有订单，不能删除 */
	    if ($group_buy['valid_order'] > 0){
	        $this->error( L('error_exist_order') );
	    }
	
	    /* 删除团购活动 */
	    $sql 			= 	"DELETE FROM " . 'ybr_goods_activity' . " WHERE act_id = '$id' LIMIT 1";
	    $CommonModel->query($sql);
	
	    admin_log(addslashes($group_buy['goods_name']) . '[' . $id . ']', 'remove', 'group_buy');	
	    //clear_cache_files();	
		$this->success("操作成功",U('groupbuy/groupbuy_list'));
	}
	
	
}







